<?php


namespace app\api\model\Course;


use app\mall\model\Merchant;
use think\facade\Db;
use think\Model;

class CourseModel extends Model
{
    /**
     * 数据表名
     * @var string
     */
    protected $name = "course";
//    protected $type=[
//        'slider_img' => 'array',
//    ];
    /**
     * 根据条件查询单条数据
     * @param $where
     * @param string $field
     * @return mixed
     * @throws
     */
    public static function getCourseWhere($where, $field = "*")
    {
        $course = Db::name("course")->field($field)->where($where)->find();
        return $course;
    }

    /**
     * 小店
     */
    public function merchant()
    {
        return $this->hasOne(Merchant::class,'id','mer_id');
    }

    /**
     * 根据条件查询多条数据
     * @param $where
     * @param $field
     * @return mixed
     * @throws
     */
    public static function getCourseWhereMore($where, $field = "c.*")
    {
        $list = Db::name("course")->alias('c')
            ->join('course_cate_relation ccr', 'ccr.course_id = c.id')
            ->join('course_cate cc', 'cc.id = ccr.cate_id')
            ->field($field)->where($where)->select()->toArray();
        return $list;
    }

    /**
     * 统计总数
     * @param $where
     * @return int
     */
    public static function getCourseCountWhere($where)
    {
        $count = Db::name("course")
            ->alias('c')
            ->join('school s', 's.id = c.school_id', 'left')
            ->join('school_area sa', 'sa.id = s.area_id', 'left')
            ->join('course_cate_relation ccr', 'ccr.course_id = c.id', 'left')
            ->where($where)
            ->count();
        return $count;
    }

    /**
     * 分页查询
     * @param $page
     * @param $limit
     * @param $where
     * @return mixed
     * @throws
     */
    public static function getCourseListWhere($page, $limit, $where = [], $field = "c.*")
    {
        $start = ceil(($page - 1) * $limit);
        $list = Db::name('course')
            ->alias('c')
            ->join('school s', 's.id = c.school_id', 'left')
            ->join('school_area sa', 'sa.id = s.area_id', 'left')
            ->join('course_cate_relation ccr', 'ccr.course_id = c.id', 'left')
            ->join('course_cate cc', 'cc.id = ccr.cate_id', 'left')
            ->field($field)
            ->where($where)
            ->limit($start, $limit)
            ->order('c.sort desc')
            ->order('c.is_group desc')
            ->select()->toArray();
        return $list;
    }

    /**
     * 添加返回ID
     * @param $data
     * @return int
     */
    public static function addCourse($data)
    {
        $id = Db::name('course')->insertGetId($data);
        return $id;
    }

    /**
     * 更新
     * @param $where
     * @param $save
     * @return int
     * @throws
     */
    public static function updateCourseWhere($where, $save)
    {
        $flag = Db::name('course')->where($where)->update($save);
        return $flag;
    }

    /**
     * 删除
     * @param $where
     * @return int
     * @throws
     */
    public static function delCourseWhere($where)
    {
        $flag = Db::name('course')->where($where)->delete();
        return $flag;
    }
}
